package com.runen.infinitemovement.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.adapter.AssociatedActivitiesAdapter;
import com.runen.infinitemovement.base.BaseActivity;
import com.runen.infinitemovement.model.api.ActivityAPI;
import com.runen.infinitemovement.model.entity.AssociatesActivitesEntity;
import com.runen.infinitemovement.tool.CommonUtil;
import com.runen.rxnetlibrary.net.callback.BaseResultCallback;
import com.runen.rxnetlibrary.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AssociatedActivitiesActivity extends BaseActivity {

    @BindView(R.id.top_btn_left)
    Button topBtnLeft;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.top_btn_right)
    Button topBtnRight;
    @BindView(R.id.top_tv_right)
    TextView topTvRight;
    @BindView(R.id.ll_title)
    LinearLayout llTitle;
    @BindView(R.id.rv_activity_list)
    RecyclerView rvActivityList;
    private AssociatedActivitiesAdapter associatedActivitiesAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_associated_activities;
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        ButterKnife.bind(this);
        topTitle.setText("关联活动");
        topBtnRight.setVisibility(View.GONE);
        topTvRight.setVisibility(View.VISIBLE);
        topTvRight.setText("确认");
        topTvRight.setTextColor(CommonUtil.getColor(R.color.white));
        topTvRight.setBackground(CommonUtil.getDrawable(R.drawable.bg_ffee4238_ffdf2526_4dp));

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvActivityList.setLayoutManager(layoutManager);
        associatedActivitiesAdapter = new AssociatedActivitiesAdapter(this);
        rvActivityList.setAdapter(associatedActivitiesAdapter);

        intiData();
        associatedActivitiesAdapter.setOnCheckClick(new AssociatedActivitiesAdapter.OnCheckClick() {
            @Override
            public void onCheckClick(AssociatesActivitesEntity.DataBean item, boolean isCheck) {
                if (isCheck) {
                    count++;
                } else {
                    count--;
                }
            }
        });

    }

    private int count;
    private String activityIds;

    private void intiData() {
        new ActivityAPI().getAssociatedActivity(new BaseResultCallback<AssociatesActivitesEntity>() {
            @Override
            public void onResponse(AssociatesActivitesEntity response) {
                if (response.code == 0) {
                    if (response.data != null && response.data.size() > 0) {
                        associatedActivitiesAdapter.setNewData(response.data);
                    }
                } else {
                    ToastUtils.showShort(response.msg);
                }
            }
        });
    }

    @OnClick({R.id.top_btn_left, R.id.top_tv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.top_btn_left:
                finish();
                break;
            case R.id.top_tv_right:
                onSure();
                break;
        }
    }

    private void onSure() {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < associatedActivitiesAdapter.getData().size(); i++) {
            if (associatedActivitiesAdapter.getData().get(i).isCheck) {
                builder.append(associatedActivitiesAdapter.getData().get(i).id + ",");
            }
        }
        if (builder.length() > 0) {
            activityIds = builder.substring(0, builder.length() - 1);
        }
        Intent intent = new Intent();
        intent.putExtra("activity_num", count);
        intent.putExtra("activity_id", activityIds);
        setResult(100, intent);
        finish();
    }
}
